﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SystemFrameWork
{
    public class ConvertHelper
    {
        /// <summary>
        /// Convert object to int without exception
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int ToInt32(object obj)
        {
            try
            {
                return Convert.ToInt32(obj);
            }
            catch (System.Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// Convert object to decimal without exception
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static double ToDouble(object obj)
        {
            try
            {
                return Convert.ToDouble(obj);
            }
            catch (System.Exception)
            {
                return 0;
            }
        }

        /// <summary>
        /// Convert object to decimal without exception
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static decimal ToDecimal(object obj)
        {
            try
            {
                return Convert.ToDecimal(obj);
            }
            catch (System.Exception)
            {
                return 0m;
            }
        }

        /// <summary>
        /// Convert Nullable decimal to decimal
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static decimal ToDecimal(Nullable<decimal> obj)
        {
            return obj.HasValue ? (decimal)obj : 0m;
        }

        /// <summary>
        /// Convert Nullable object to pure value
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T ToNotNullValue<T>(Nullable<T> obj) where T : struct
        {
            if (obj.HasValue)
            {
                return obj.Value;
            }
            return default(T);
        }

        /// <summary>
        /// Convert value to given precision decimal number with given custom
        /// </summary>
        /// <param name="value"></param>
        /// <param name="custom"></param>
        /// <returns></returns>
        public static object ToDecimal(object value, string custom)
        {
            if (string.IsNullOrEmpty(custom))
            {
                return ToDecimal(value);
            }

            int precision = custom.Length - custom.LastIndexOf(".") - 1;
            if (custom.IndexOf("%") >= 0)
            {
                // #,##0.00%
                // presision is 4
                // 3++
                precision++;
            }

            try
            {
                return Math.Round(Convert.ToDecimal(value), precision);
            }
            catch (System.Exception)
            {
                return value;
            }
        }
    }
}
